﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.util.collision;
using Imaginecup.factory;
using Imaginecup.util;
using imaginecup.util;
using Imaginecup.sprite;
using Imaginecup.util.resourceMan;

namespace Imaginecup.gameState.temp
{
    public class MenuState : GameState
    {
        private ContentManager m_MenuContent;
        private Texture2D m_Background;
        private Texture2D m_MenuTexture;

        private Vector2 m_Size = new Vector2(307, 368);
        private Vector2 m_Pos = new Vector2(247, 56);

        private const int MAX_MENU = 3;

        private Vector2 m_MenuBlockSize;
        private Vector2[] m_MenuPos;

        private static MenuState m_MenuState = null;

        private MenuState()
        {
            m_MenuContent = new ContentManager(m_Game.Services);
            m_MenuContent.RootDirectory = "Content";
        }



        public static MenuState GetMenuState()
        {
            if (m_MenuState == null)
            {
                m_MenuState = new MenuState();
            }
            return m_MenuState;
        }


        /************************************************************************/
        /* 초기화 되는 값(메뉴인덱스)                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            m_MenuBlockSize = new Vector2(210, 65);

            m_MenuPos = new Vector2[MAX_MENU];
            m_MenuPos[0] = new Vector2(50 + m_Pos.X, 145 + m_Pos.Y);

            for (int i = 1; i < MAX_MENU; i++)
            {
                m_MenuPos[i] = new Vector2(m_MenuPos[i - 1].X, m_MenuPos[i - 1].Y + m_MenuBlockSize.Y);
            }


        }


        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            m_Background = m_MenuContent.Load<Texture2D>("menu\\play_stop");
            m_MenuTexture = m_MenuContent.Load<Texture2D>("menu\\stop_menu");
        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            m_MenuContent.Unload();
        }



        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {

#if WINDOWS_PHONE
            IPhoneInputHandler inputHandler = PhoneInputHandler.GetInputHandler();

            IPhoneInputHandler input = PhoneInputHandler.GetInputHandler();

            TouchCollection collection = input.GetCurrentTouch();

           
            if (collection.Count > 0)
            {
                foreach (TouchLocation touch in collection)
                {
                    if (touch.State == TouchLocationState.Pressed)
                    {

                        for( int i = 0 ; i < MAX_MENU ; i++ )
                        {
                            if(  Collision.ContainPoint( m_MenuPos[i] , m_MenuBlockSize , touch.Position ) )
                            {
                                StateChange(i);
                                return;
                            }
                        }
                    }
                }
            }
#endif

#if WINDOWS|| XBOX
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

            if (mouseInput.IsOneClick())
                for (int i = 0; i < MAX_MENU; i++)
                {
                    if (Collision.ContainPoint(m_MenuPos[i], m_MenuBlockSize, mouseInput.Position))
                    {
                        StateChange(i);
                        return;
                    }
                }
#endif
            base.Update(gameTime);
        }

        private void StateChange(int menuIndex)
        {
            switch (menuIndex)
            {
                case 0:
                    m_GameStateMan.PopState();
                    return;
                case 1:
                    m_GameStateMan.PopAll();
                    ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
                    return;
                case 2:
                    m_GameStateMan.PopAll();
                    m_Game.Exit();
                    //m_GameStateMan.pushState((GameState)m_Game.Services.GetService(typeof(IEndState)));
                    return;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();
            sb.Begin();
            sb.Draw(m_Background, Vector2.Zero, Color.White);
            sb.Draw(m_MenuTexture, m_Pos,
                new Rectangle(0, 0, (int)m_Size.X, (int)m_Size.Y), Color.White);
            sb.End();
            base.Draw(gameTime);
        }
    }
}
